<template>
	<view>
		<!-- 订单详情 -->
		<!-- 步骤 -->
		<steps :message="orderMessage"></steps>
		<j-load v-if="loading"></j-load>
		<!-- 内容 -->
		<scroll-view v-else scroll-y>
      <order-lists :list="list" :data="orderMessage"></order-lists>
		</scroll-view>
		<view class="height-xs"></view>
	</view>
</template>

<script>
  import orderLists from "../components/uni-lists/index.vue"
  import logins from '@/utils/loginApi.js'
  import { getOrderDetail } from '@/API/api.js'
  import { getLocalDate } from '@/utils/index.js'
  import constants from '@/utils/constants.js'
  import orderStatus from "@/jvss/pageCloud/utils/pay"
  import steps from './module/details/steps.vue'
  const { handleLogin, httpReturn } = logins();
  const { PAYTYPE } = constants;
  const { orderStatusTime } = orderStatus();

  export default {
    components: {
      steps,
      orderLists
    },
    data() {
      return {
        loading: false, //加载
        stepList: [], //步骤列表
        orderMessage: {},//内容数据
        list: [],
        urlParams:{
          referer:'',
        }
      }
    },
    onLoad(e) {
      const {orderId,referer} = e;
      this.urlParams.referer = referer;
      handleLogin(() => {
        this.handleOrderDetail(orderId)
      })
    },
    onShow() {

    },
    methods: {
      handleOrderDetail (orderId) {
        this.loading = true;
        getOrderDetail({orderId}).then(res => {
          httpReturn(res, (data) => {
            this.orderMessage = data;
            const {currencyUnit,price,discount,couponAmount,integralAmount,balance,createMillis,payMillis,openMillis,payType,totalAmount} = this.orderMessage;
            const { status,deviceId } = data;
            // '待开通' 和 '待支付' 定时查询支付状态
            if (status === "pay" || status === "create") {
              orderStatusTime(orderId, () => {
                this.handleOrderDetail(orderId)
              });
            }
            let list = []
            let listObj = {
              'deviceId': {label: this.$t('JLink.order.details.message.deviceId'),key: "deviceId",text: '', class: ''},
              'tradeNo': {label: this.$t('JLink.order.details.message.tradeNo'),key: "tradeNo",text: '', class: ''},
              'price': {label: this.$t('JLink.order.details.message.price'),key: "price",text: '', class: ''},
              'discount': {label: this.$t('JLink.order.details.message.discount'),key: "discount",text: '',class: 'text-red'},
              'couponAmount': {label: this.$t('JLink.order.details.message.couponAmount'),key: "couponAmount",text: '',class: 'text-red'},
              'integralAmount': {label: this.$t('JLink.integral.deduct'),key: "integralAmount",text: '',class: 'text-red'},
              'balance':{label: this.$t('JLink.order.details.message.balance'),key: "balance",text: '',class: 'text-red'},
              'createMillis': {label: this.$t('JLink.order.details.message.createMillis'),key: "createMillis",text: '',class: ''},
              'payMillis':{label: this.$t('JLink.order.details.message.payMillis'),key: "payMillis",text: ''},
              'openMillis':{label: this.$t('JLink.order.details.message.openMillis'),key: "openMillis",text: ''},
              'payType': {label: this.$t('JLink.order.details.message.payType'),key: "payType",text: ''},
              'totalAmount': {label: this.$t('JLink.order.details.message.totalAmount'),key: "totalAmount",text: '',class: 'text-red'},
            }
            Object.keys(this.orderMessage).map((item) => {
              switch(item){
                  //序列号
                case 'deviceId':
                  listObj['deviceId']['text'] = data[item]
                  break
                  //订单编号
                case 'tradeNo':
                  listObj['tradeNo']['text'] = data[item]
                  break
                  //价格
                case 'price':
                  listObj['price']['text'] = currencyUnit ? currencyUnit + price : ''
                  break
                  //平台优惠
                case 'discount':
                  listObj['discount']['text'] = currencyUnit ? currencyUnit + discount : ''
                  break
                  //优惠券
                case 'couponAmount':
                  let couponAmount_ = couponAmount ?
                      Number(couponAmount).toFixed(2) :
                      "0.00";
                  listObj['couponAmount']['text'] = currencyUnit ? currencyUnit + couponAmount_ :''
                  break
                  //积分抵扣
                case 'integralAmount':
                  listObj['integralAmount']['text'] = integralAmount ? currencyUnit + integralAmount :''
                  break
                  //原套餐抵扣
                case 'balance':
                  listObj['balance']['text'] = currencyUnit ?  currencyUnit + balance : ''
                  break
                  //下单时间
                case 'createMillis':
                  listObj['createMillis']['text'] = createMillis ? getLocalDate(createMillis) : ""
                  break
                  //支付时间
                case 'payMillis':
                  listObj['payMillis']['text'] = payMillis ? getLocalDate(payMillis) : ""
                  break
                  //开通时间
                case 'openMillis':
                  listObj['openMillis']['text'] = openMillis ? getLocalDate(openMillis) : ""
                  break
                  //支付方式
                case 'payType':
                  let name;
                  PAYTYPE().forEach((t) => {
                    if (t.value == payType) {
                      name = t.label;
                    }
                  });
                  listObj['payType']['text'] = name
                  break
                  //已付款
                case 'totalAmount':
                  let totalAmount_ = totalAmount ?
                      Number(totalAmount).toFixed(2) :
                      "0.00";
                  listObj['totalAmount']['text'] = currencyUnit ? currencyUnit + totalAmount_ : ''
                  break
              }
            })
            Object.keys(listObj).map((item) => {
              list.push(listObj[item])
            })
            this.list = list
          }, (code) => {})
        }).catch(()=>{}).finally(() => this.loading = false)
      }
    }
  }
</script>

<style lang="less" scope>
@import '@/common/style/main.css';
</style>
